package com.example.beson.androiddemo.login.mvc.controller;

import com.example.beson.androiddemo.login.mvc.model.UserInfo;
import com.example.beson.androiddemo.login.mvc.view.MVCILoginView;
import com.example.beson.androiddemo.utils.CheckedIsPhoneNumUtil;
import com.example.beson.androiddemo.utils.StringUtils;

/**
 * Created by 史国院 on 2017/4/25.
 * Desc 定义方法以及处理逻辑，
 * 与 MVCILoginView 关联。这就满足了 MVC 架构中 Controller 同时与 View 和 Model 交互的原则。
 */

public class MVCLoginController{
    private MVCILoginView mMVCILoginView;

    public MVCLoginController(MVCILoginView mMVCILoginView) {
        this.mMVCILoginView = mMVCILoginView;
    }

    public void LoginMethod(String phone, String pwd) {
        UserInfo userInfo=new UserInfo();
        userInfo.setPhoneNum(phone);
        userInfo.setPassword(pwd);
        if(phone.equals("18653091256")){
            mMVCILoginView.loginSuccess(userInfo);
        }else{
            mMVCILoginView.loginFaild(userInfo);
        }
        mMVCILoginView.dismissLoading();
    }

    public boolean checkPhoneAndPwd(String phone, String pwd) {
        mMVCILoginView.showLoading();
        if(StringUtils.isEmpty(phone)){
            mMVCILoginView.phoneError("手机号不能为空！");
            mMVCILoginView.dismissLoading();
            return false;
        }else{
            if(!CheckedIsPhoneNumUtil.checkphone(phone)){
                mMVCILoginView.phoneError("手机号格式不正确！");
                mMVCILoginView.dismissLoading();
                return false;
            }
        }
        if(StringUtils.isEmpty(pwd)){
            mMVCILoginView.passwordError("密码不能为空！");
            mMVCILoginView.dismissLoading();
            return false;
        }
        return true;
    }
}
